home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1190 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  65 lines

  1. Path: news.umbc.edu!not-for-mail
  2. From: schlein@umbc.edu (Jonas J. Schlein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Comma Delimited function wanted
  5. Date: 11 Jan 1996 21:21:00 -0500
  6. Organization: University of Maryland Baltimore County
  7. Message-ID: <4d4gic$p7u@umbc9.umbc.edu>
  8. References: <4d1l42$mb6@voyager.Internex.NET>
  9. NNTP-Posting-Host: f-umbc9.umbc.edu
  10. NNTP-Posting-User: schlein
  11.  
  12. Ian H. Stewart <ian_stewart@nyro.com> wrote:
  13. |> Help with a comma delimited function needed.
  14. |> 
  15. |> I did this once before, but I am blocked and can't
  16. |> find the original source.
  17. |> 
  18. |> Here is what I need to do:
  19. |> 
  20. |> I have a buffer full of text.
  21. |> 
  22. |> char buffer = "3740067099,914885AC2,P03,5000";
  23.  
  24. That should be a char *...
  25.  
  26. |> I have four char vars to put this into.
  27. |> 
  28. |> char field1[20], field2[20], field3[20], field4[20];
  29. |> 
  30. |> What I want to do is get this as the end result.
  31. |> 
  32. |> field1 = 3740067099
  33. |> field2 = 914885AC2
  34. |> field3 = P03
  35. |> field4 = 5000
  36. |> 
  37. |> Anyway, help is appreciated.
  38.  
  39. I'd think sscanf() is the easiest solution although strtok() would work
  40. as well as custom writing a character by character scanning function.
  41. Here's a little example using sscanf(). I hope I got the scanset right ;-).
  42.  
  43. #include <stdio.h>
  44.  
  45. int main (void)
  46. {
  47.   char *buffer = "3740067099,914885AC2,P03,5000";  
  48.   char  field1[20],
  49.         field2[20],
  50.         field3[20],
  51.         field4[20];
  52.  
  53.   sscanf (buffer, "%[^,],%[^,],%[^,],%s", field1, field2, field3, field4);
  54.   printf ("field1 = %s\n", field1);
  55.   printf ("field2 = %s\n", field2);
  56.   printf ("field3 = %s\n", field3);
  57.   printf ("field4 = %s\n", field4);
  58.  
  59.   return (0);
  60. }
  61. -- 
  62. "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
  63.  
  64. Jonas J. Schlein  (schlein@gl.umbc.edu)
  65.